<?php

namespace App\Models\Landscaping;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Landscaping\Landscaping;
use App\Models\Landscaping\Vegetation;

class ParkProtect extends Model
{
	use SoftDeletes;
	protected $guarded = [];

    //查询list
    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
        $landscaping = new Landscaping;
        return $landscaping->whereHas('vegetation',function($query) use ($request){
                    $vegetation_type = $request->input('vegetation_type');//植被类型
                    if(!empty($vegetation_type)) {
                        $query->where('type',$vegetation_type);
                    }
                })
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含park_name、park_number参数
                    $park_name = $request->input('park_name');//园林名称
                    $park_number = $request->input('park_number');//园林编号
                    $protect_principal_id = $request->input('protect_principal_id','');

                    //时间区间条件
                    $begin_time = $request->input('begin_time');
                    $end_time = $request->input('end_time');

                    if(!empty($park_name)) {
                        $query->where('park_name','like','%'.$park_name.'%');
                    }
                    if(!empty($park_number)) {
                        $query->where('park_number','=',$park_number);
                    }
                    if(!empty($protect_principal_id)) {
                        $query->where('protect_principal_id','=',$protect_principal_id);
                    }
                    if(!empty($begin_time) && !empty($end_time))
                    {
                        $query->whereBetween('created_at',[$begin_time,$end_time]);
                    }

                })
                ->select('id','park_name','park_number','park_address','vegetation_id','vegetation_yh_areas')
                ->with(['vegetation','parkProtect'=>function($query){
                    $query->with('employees');
                }])
                ->paginate($per_page);
    }

    //查看负责人
    public function employees()
    {
        return $this->belongsTo(\App\Models\SystemManagement\Employee::class,'protect_principal_id','id')->select('id','name');
    }

}
